﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDPhieuDatHangCT
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dedongia;
        private long _deid;
        private int _deiddangnhap;
        private int _deidduocpham;
        private long _deidphieu;
        private decimal _desoluong;
        private decimal _desoluongdagiao;
        private decimal _desoluongtong;
        private int _idquydoi;
        private string _shangsx;
        private string _sngaycapnhat;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDPhieuDatHangCT()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDPhieuDatHangCT(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDPhieuDatHangCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand("delete from " + this._sSchema + "." + tableName + " where 1=1  ", connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDPhieuDatHangCT.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDPhieuDatHangCT.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDPhieuDatHangCT.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDPhieuDatHangCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDPHIEU, ",", columnIDDUOCPHAM, ",", columnSOLUONG, ",", columnSOLUONGTONG, ",", columnIDQUYDOI, 
                    ",", columnHANGSX, ",", columnTENMAYTINH, ",", columnNGAYCAPNHAT, ",", columnIDDANGNHAP, ",", columnSOLUONGDAGIAO, ",", columnDONGIA, ") values(", this._deid, ",", this._deidphieu, 
                    ",", this._deidduocpham, ",:", columnSOLUONG, ",:", columnSOLUONGTONG, ",", this._idquydoi, ",:", columnHANGSX, ",:", columnTENMAYTINH, ",now(),", this._deiddangnhap, ",:", columnSOLUONGDAGIAO, 
                    ",:", columnDONGIA, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnHANGSX, NpgsqlDbType.Text, 200).Value = this._shangsx;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 20).Value = this._desoluong;
                command.Parameters.Add(columnSOLUONGTONG, NpgsqlDbType.Numeric, 20).Value = this._desoluongtong;
                command.Parameters.Add(columnSOLUONGDAGIAO, NpgsqlDbType.Numeric, 20).Value = this._desoluongdagiao;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 20).Value = this._dedongia;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDPhieuDatHangCT.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._deidphieu = 0L;
            this._deidduocpham = 0;
            this._desoluong = 0M;
            this._desoluongtong = 0M;
            this._idquydoi = 0;
            this._shangsx = "";
            this._stenmaytinh = "";
            this._sngaycapnhat = "";
            this._deiddangnhap = 0;
            this._desoluongdagiao = 0M;
            this._dedongia = 0M;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDPhieuDatHangCT.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnIDPHIEU, ",", columnIDDUOCPHAM, ",", columnSOLUONG, ",", columnSOLUONGTONG, ",", columnIDQUYDOI, ",", columnHANGSX, ",", columnTENMAYTINH, 
                    ",to_char(", columnNGAYCAPNHAT, ",'dd/mm/yyyy') as ", columnNGAYCAPNHAT, ",", columnIDDANGNHAP, ",", columnSOLUONGDAGIAO, ",", columnDONGIA, " from ", this._sSchema, ".", tableName, " where ", columnID, 
                    " = ", this._deid
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                    this._deidphieu = long.Parse(dataTable.Rows[0][columnIDPHIEU].ToString());
                    this._deidduocpham = int.Parse(dataTable.Rows[0][columnIDDUOCPHAM].ToString());
                    this._desoluong = decimal.Parse(dataTable.Rows[0][columnSOLUONG].ToString());
                    this._desoluongtong = decimal.Parse(dataTable.Rows[0][columnSOLUONGTONG].ToString());
                    this._idquydoi = int.Parse(dataTable.Rows[0][columnIDQUYDOI].ToString());
                    this._shangsx = dataTable.Rows[0][columnHANGSX].ToString();
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycapnhat = dataTable.Rows[0][columnNGAYCAPNHAT].ToString();
                    this._deiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._desoluongdagiao = decimal.Parse(dataTable.Rows[0][columnSOLUONGDAGIAO].ToString());
                    this._dedongia = decimal.Parse(dataTable.Rows[0][columnDONGIA].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDPhieuDatHangCT.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDPhieuDatHangCT.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDPHIEU + "," + columnIDDUOCPHAM + "," + columnSOLUONG + "," + columnSOLUONGTONG + "," + columnIDQUYDOI + "," + columnHANGSX + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCAPNHAT + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCAPNHAT + "," + columnIDDANGNHAP + "," + columnSOLUONGDAGIAO + "," + columnDONGIA + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDPhieuDatHangCT.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDPhieuDatHangCT.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnIDPHIEU, " = ", this._deidphieu, ",", columnIDDUOCPHAM, " = ", this._deidduocpham, ",", columnSOLUONG, " = :", columnSOLUONG, 
                    ",", columnSOLUONGTONG, " = :", columnSOLUONGTONG, ",", columnIDQUYDOI, " = ", this._idquydoi, ",", columnHANGSX, " = :", columnHANGSX, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, 
                    ",", columnNGAYCAPNHAT, " = now(),", columnIDDANGNHAP, " = ", this._deiddangnhap, ",", columnSOLUONGDAGIAO, " = :", columnSOLUONGDAGIAO, ",", columnDONGIA, " = :", columnDONGIA, " where ", columnID, 
                    " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnHANGSX, NpgsqlDbType.Text, 200).Value = this._shangsx;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnSOLUONG, NpgsqlDbType.Numeric, 20).Value = this._desoluong;
                command.Parameters.Add(columnSOLUONGTONG, NpgsqlDbType.Numeric, 20).Value = this._desoluongtong;
                command.Parameters.Add(columnSOLUONGDAGIAO, NpgsqlDbType.Numeric, 20).Value = this._desoluongdagiao;
                command.Parameters.Add(columnDONGIA, NpgsqlDbType.Numeric, 20).Value = this._dedongia;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDPhieuDatHangCT.cs");
                return false;
            }
        }

        public static string columnDONGIA
        {
            get
            {
                return "dongia";
            }
        }

        public static string columnHANGSX
        {
            get
            {
                return "hangsx";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDDUOCPHAM
        {
            get
            {
                return "idduocpham";
            }
        }

        public static string columnIDPHIEU
        {
            get
            {
                return "idphieu";
            }
        }

        public static string columnIDQUYDOI
        {
            get
            {
                return "idquydoi";
            }
        }

        public static string columnNGAYCAPNHAT
        {
            get
            {
                return "ngaycapnhat";
            }
        }

        public static string columnSOLUONG
        {
            get
            {
                return "soluong";
            }
        }

        public static string columnSOLUONGDAGIAO
        {
            get
            {
                return "soluongdagiao";
            }
        }

        public static string columnSOLUONGTONG
        {
            get
            {
                return "soluongtong";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public decimal pDONGIA
        {
            get
            {
                return this._dedongia;
            }
            set
            {
                this._dedongia = value;
            }
        }

        public string pHANGSX
        {
            get
            {
                return this._shangsx;
            }
            set
            {
                this._shangsx = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._deiddangnhap;
            }
            set
            {
                this._deiddangnhap = value;
            }
        }

        public int pIDDUOCPHAM
        {
            get
            {
                return this._deidduocpham;
            }
            set
            {
                this._deidduocpham = value;
            }
        }

        public long pIDPHIEU
        {
            get
            {
                return this._deidphieu;
            }
            set
            {
                this._deidphieu = value;
            }
        }

        public int pIDQUYDOI
        {
            get
            {
                return this._idquydoi;
            }
            set
            {
                this._idquydoi = value;
            }
        }

        public string pNGAYCAPNHAT
        {
            get
            {
                return this._sngaycapnhat;
            }
            set
            {
                this._sngaycapnhat = value;
            }
        }

        public decimal pSOLUONG
        {
            get
            {
                return this._desoluong;
            }
            set
            {
                this._desoluong = value;
            }
        }

        public decimal pSOLUONGDAGIAO
        {
            get
            {
                return this._desoluongdagiao;
            }
            set
            {
                this._desoluongdagiao = value;
            }
        }

        public decimal pSOLUONGTONG
        {
            get
            {
                return this._desoluongtong;
            }
            set
            {
                this._desoluongtong = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_phieudathangct";
            }
        }
    }
}

